package com.hiquety.tcb.disk;

import com.alibaba.fastjson.JSON;
import com.hiquety.tcb.config.AppInfo;
import com.hiquety.tcb.config.ClipboardSystem;
import com.hiquety.tcb.entity.AppConf;
import org.apache.commons.io.FileUtils;

import java.io.IOException;

public class AppConfDisk {

    public static AppConf read() {
        AppConf conf;
        try {
            if (AppInfo.CONF_FILE.exists()) {
                String s = FileUtils.readFileToString(AppInfo.CONF_FILE, "utf-8");
                return JSON.parseObject(s, AppConf.class);
            } else {
                conf = new AppConf();
                String s = JSON.toJSONString(conf);
                FileUtils.write(AppInfo.CONF_FILE, s, "utf-8");
                return conf;
            }
        } catch (IOException e) {
            conf = new AppConf();
            return conf;
        }
    }

    public static void write() {
        try {
            String s = JSON.toJSONString(ClipboardSystem.appConf);
            FileUtils.write(AppInfo.CONF_FILE, s, "utf-8");
        } catch (Exception e) {
        }
    }
}
